package cn.zjh.basic.util;

import cn.zjh.basic.jwt.JwtUtils;
import cn.zjh.basic.jwt.LoginData;
import cn.zjh.basic.jwt.Payload;
import cn.zjh.basic.jwt.RsaUtils;
import cn.zjh.user.domain.Logininfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {

    public static Logininfo getLoginUser(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            if (StringUtils.isEmpty(token)){
                return null;
            }
            //获取公钥
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
            //获取数据
            Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
            LoginData loginData = payload.getLoginData();
            if (loginData==null){
                return null;
            }
            return loginData.getLogininfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }


}
